package edu.fdzc.controller;

import edu.fdzc.pojo.PageResult;
import edu.fdzc.pojo.Result;
import edu.fdzc.pojo.Student;
import edu.fdzc.pojo.StudentQueryParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import edu.fdzc.service.StudentService;

import java.util.List;

@Slf4j
@RequestMapping("/students")
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 学员列表查询
     */
    @GetMapping
    public Result list(StudentQueryParam studentQueryParam) {
        log.info("查询学员列表");
        PageResult<Student> pageResult = studentService.list(studentQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 新增学员
     */
    @PostMapping
    public Result save(@RequestBody Student student) {
        log.info("新增学员");
        studentService.save(student);
        return Result.success();
    }

    /**
     * 根据id查询学员
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询学员");
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    /**
     * 修改学员
     */
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("修改学员");
        studentService.update(student);
        return Result.success();
    }

    /**
     * 批量删除学员
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("批量删除学员, ids: {}", ids);
        studentService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 违纪处理
     * 请求路径：/students/violation/{id}/{score}
     * 请求方式：PUT
     */
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Short score) {
        log.info("违纪处理, id: {}, score: {}", id, score);
        studentService.violation(id, score);
        return Result.success();
    }
}
